<?php
/**
 * Created by PhpStorm.
 * User: nightelf
 * Date: 2020/1/15
 * Time: 10:56
 */

namespace App\Http\Model;


use Illuminate\Support\Facades\DB;

class ProjectAreaModel
{
    const TABLE = 'project_area';

    /**
     * @param array $data
     * @param bool $exports
     * @return mixed
     */
    public function lists(array $data,$exports = false)
    {
        $result = DB::table(self::TABLE)
            ->leftJoin('project as p','p.id','=',self::TABLE.'.projectId')
            ->where(function ($query) use ($data) {
                $query->where('projectId', $data['projectId']);
                if (isset($data['search']) && !is_null($data['search'])) {
                    $query->where(self::TABLE.'.name', 'like', '%' . $data['search'] . '%');
                }
            })
            ->orderBy('order','desc')
            ->select(self::TABLE.'.*','p.name as projectName');
        if(!$exports){
            $limit = config('yucheng.limit');
            $start = 0;
            if (!empty($data['length'])) {
                $limit = $data['length'];
            }
            if (!empty($data['page'])) {
                $start = ($data['page'] - 1) * $limit;
            }
            $result = $result->offset($start)->limit($limit);
        }
        $result = $result->get()->toArray();
        return $result;
    }

    /**
     * @param array $data
     * @return mixed
     */
    public function countLists(array $data)
    {
        return DB::table(self::TABLE)
            ->leftJoin('project as p','p.id','=',self::TABLE.'.projectId')
            ->where(function ($query) use ($data) {
                $query->where('projectId', $data['projectId']);
                if (isset($data['search']) && !is_null($data['search'])) {
                    $query->where(self::TABLE.'.name', 'like', '%' . $data['search'] . '%');
                }
            })
            ->select(self::TABLE.'.*','p.name as projectName')
            ->count();
    }

    /**
     * @param array $check
     * @param $id
     * @return array
     */
    public function checkRepeat(array $check, $id = 0)
    {
        $result = DB::table(self::TABLE)->where($check)
            ->where(function ($query) use ($id){
                if ($id != 0){
                    $query->where('id','!=',$id);
                }
            })
            ->first();
        return empty($result) ? [] : get_object_vars($result);
    }

    /**
     * @param array $data
     * @return mixed
     */
    public function insert(array $data)
    {
        $data['createTime'] = date('Y-m-d H:i:s');
        return DB::table(self::TABLE)->insert($data);
    }

    /**
     * @param array $data
     * @return array
     */
    public function batchInfo(array $data)
    {
        $result = DB::table(self::TABLE)->where('name','like','%号楼')->where('projectId',$data['projectId'])->orderBy('id', 'desc')->first();
        return empty($result) ? [] : get_object_vars($result);
    }

    /**
     * @param array $data
     * @return mixed
     */
    public function getMaxOrder(array $data)
    {
        return DB::table(self::TABLE)->where($data)->max('order');
    }

    /**
     * @param array $data
     * @return array
     */
    public function info(array $data)
    {
        $result = DB::table(self::TABLE)->where($data)->first();
        return empty($result) ? [] : get_object_vars($result);
    }

    /**
     * @param array $where
     * @param array $data
     * @return mixed
     */
    public function update(array $where,array $data)
    {
        return DB::table(self::TABLE)->where($where)->update($data);
    }

    /**
     * @param array $data
     * @return mixed
     */
    public function delete(array $data)
    {
        return DB::table(self::TABLE)->where($data)->delete();
    }

    /**
     * @param array $input
     * @return mixed
     */
    public function selectLists(array $input)
    {
        return DB::table(self::TABLE)
            ->where(function ($query) use ($input) {
                $query->where('projectId', $input['projectId']);
            })
            ->orderBy('order','desc')
            ->get()->toArray();
    }

    /**
     * 更新施工区的面积
     *
     * @param $areaId
     * @return mixed
     */
    public function updateAreaArea($areaId)
    {
        $sum = (new ProjectSectionModel)->sumArea($areaId);
        return $this->update(['id'=>$areaId],['area'=>$sum['area'],'formworkArea'=>$sum['formworkArea']]);
    }
}